package com.kcht.itsed.mws.commmodel;

import java.io.Serializable;

/**
 * CRAP元组
 * 用于标识物理设备的最小单位
 * Class	阅读器种类
 * Reader	设备标识
 * Antenna	天线号
 * Protocol	协议
 * 
 * 字符串表示：LRName[所属逻辑阅读器]:Class[阅读器类型]:ID[阅读器ID]:IP[阅读器地址]:Port[阅读器端口]:Ant[天线号]:Protocol[协议]
 * 其中根据不同的场合，有些可以空
 * @author 张文硕
 *
 */
public class CRAP implements Serializable{

	private static final long serialVersionUID = 1;
	
	private String lrName;	//逻辑阅读器名称
	private String deviceClass;
	private String readerIp;
	private String readerId;
	private int readerPort;
	private int antenna;
	private String protocol;	//协议类型可以扩展，预定义两种：EPC/6B
	
	
	public CRAP() {
	}
	
	public CRAP(String readerId, int antenna, String protocol) {
		super();
		this.readerId = readerId;
		this.antenna = antenna;
		this.protocol = protocol;
	}

	public CRAP(String desc){
		for(String kv:desc.split(":")){
			int d = kv.indexOf("[");
			setValue(kv.substring(0,d),kv.substring(d+1,kv.length()-1));
		}
	}
	
	private void setValue(String key,String value){
		switch(key){
		case "LRName":this.setLrName(value);break;
		case "Class":this.setDeviceClass(value);break;
		case "ID":this.setReaderId(value);break;
		case "IP":this.setReaderIp(value);break;
		case "readerPort":this.setReaderPort(Integer.parseInt(value));break;
		case "Ant":this.setAntenna(Integer.parseInt(value));break;
		case "Protocol":this.setProtocol(value);break;
		}
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + antenna;
		result = prime * result + ((protocol == null) ? 0 : protocol.hashCode());
		result = prime * result + ((readerId == null) ? 0 : readerId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CRAP other = (CRAP) obj;
		if (antenna != other.antenna)
			return false;
		if (protocol == null) {
			if (other.protocol != null)
				return false;
		} else if (!protocol.equals(other.protocol))
			return false;
		if (readerId == null) {
			if (other.readerId != null)
				return false;
		} else if (!readerId.equals(other.readerId))
			return false;
		return true;
	}

	@Override
	public String toString(){
		return String.format("LRName[%s]:Class[%s]:ID[%s]:IP[%s]:Ant[%d]:Protocol[%s]",
				lrName,deviceClass,readerId,readerIp,antenna,protocol);
		
	}
	public int getReaderPort() {
		return readerPort;
	}

	public void setReaderPort(int readerPort) {
		this.readerPort = readerPort;
	}

	public String getLrName() {
		return lrName;
	}
	public void setLrName(String lrName) {
		if(lrName == null || lrName.equals("null"))return;
		this.lrName = lrName;
	}
	public String getDeviceClass() {
		return deviceClass;
	}
	public void setDeviceClass(String deviceClass) {
		this.deviceClass = deviceClass;
	}
	public String getReaderIp() {
		return readerIp;
	}
	public void setReaderIp(String readerIp) {
		this.readerIp = readerIp;
	}
	public String getReaderId() {
		return readerId;
	}
	public void setReaderId(String readerId) {
		this.readerId = readerId;
	}
	public int getAntenna() {
		return antenna;
	}
	public void setAntenna(int antenna) {
		this.antenna = antenna;
	}
	public String getProtocol() {
		return protocol;
	}
	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}
}
